<template>
  <div class="create-or-edit">
    <el-form>
      <el-form-item label="资源名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="资源路径">
        <el-input v-model="form.url"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input
          type="textarea"
          v-model="form.description"
          ></el-input>
      </el-form-item>
      <el-form-item>
          <el-button @click="onCancel">取消</el-button>
          <el-button
          type="primary"
          @click="onSubmit"
          >确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createOrUpdate, getResourceById } from '@/services/resource'

export default {
  nama: 'CreateOrEdit',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    resourceId: {
      type: [Number, String]
    }
  },
  data () {
    return {
      form: {
        name: '',
        url: '',
        description: ''
      }
    }
  },
  created () {
    this.loadResourceId()
  },
  methods: {
    async loadResourceId () {
      const { data } = await getResourceById(this.resourceId)
      if (data.code === '000000') {
        this.form = data.data
      }
    },
    onCancel () {
      // 设置取消状态，让父组件处理
      this.$emit('cancel')
    },
    async onSubmit () {
      const { data } = await createOrUpdate(this.form)
      if (data.code === '000000') {
        // 关闭提示(需要子向父组件传递状态)
        this.$emit('success')
        // 设置成功提示
        this.$message.success('添加成功')
        // 清除表单内容
        this.form = {}
      }
    }
  }
}
</script>

<style>

</style>
